package com.nulldev.util.java.swing.extSwing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Caret;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

import com.nulldev.util.java.JVMUtil;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.Theme;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.java.swing.font.FontManager;

import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.JEditorPane;

import java.awt.event.FocusEvent;

public class eIDEditor extends JFrame {

	private JPanel contentPane;
	private JEditorPane dtrpnNullide;

	public static void main(String[] args) {
		main();
	}
	
	/**
	 * Launch the application.
	 */
	public static eIDEditor main() {
		eIDEditor frame = new eIDEditor();
		ThemeApplier.applyTheme(frame);
		frame.setVisible(true);
		return frame;
	}

	/**
	 * Create the frame.
	 */
	public eIDEditor() {
		setTitle("extSwing.eIDEEditor");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 807, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
        final Caret caret = new DefaultCaret()
        {
            public void focusGained(final FocusEvent e)
            {
                setVisible(true);
                setSelectionVisible(true);
                setForeground(Color.YELLOW);
            }
        };
        caret.setBlinkRate(UIManager.getInt("TextField.caretBlinkRate"));
		
		dtrpnNullide = new JEditorPane() {
			
			@Override
			public void setFocusable(final boolean state) {
				super.setFocusable(true);
			}
			
			@Override
			public void setFont(final Font font) {
				super.setFont(FontManager.getMimeFont(14));
			}
		};
		dtrpnNullide.setCaret(caret);
		dtrpnNullide.setFocusTraversalKeysEnabled(false);

		dtrpnNullide.setText("");
		dtrpnNullide.setCaretColor(Color.YELLOW);
		
		final Document doc = dtrpnNullide.getDocument();
		if (doc instanceof PlainDocument) {
		    doc.putProperty(PlainDocument.tabSizeAttribute, 2);
		}
		scrollPane.setViewportView(dtrpnNullide);
		
		JPanel panel = new JPanel();
		scrollPane.setColumnHeaderView(panel);
	}

	private void appendCns(final String cns) {
		if (dtrpnNullide.getText() == null || dtrpnNullide.getText().length() == 0) {
			dtrpnNullide.setText(cns);
		}else {
			dtrpnNullide.setText(dtrpnNullide.getText() + JVMUtil.lineSep() + cns);
		}
	}
	
	public JEditorPane getIDEWriteArea() {
		return dtrpnNullide;
	}
}
